
package es.udc.emarketapp.emarket.model.categoryFacade;

import javax.sql.DataSource;

import es.udc.emarketapp.emarket.model.category.vo.CategoryVO;
import es.udc.emarketapp.emarket.model.categoryFacade.actions.CreateCategoryAction;
import es.udc.emarketapp.emarket.model.categoryFacade.actions.DeleteCategoryAction;
import es.udc.emarketapp.emarket.model.categoryFacade.actions.FindCategoryByIdAction;
import es.udc.emarketapp.emarket.model.categoryFacade.actions.FindCategoryByNameAction;
import es.udc.emarketapp.emarket.model.categoryFacade.actions.GetCategoryRouteAction;
import es.udc.emarketapp.emarket.model.categoryFacade.actions.GetSonsCategoryAction;
import es.udc.emarketapp.emarket.model.categoryFacade.actions.ModifyCategoryAction;
import es.udc.emarketapp.emarket.model.categoryFacade.vo.CategoryChunkVO;
import es.udc.emarketapp.emarket.util.exceptions.InstanceCannotBeDeletedException;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.PlainActionProcessor;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;
import java.util.List;


public class CategoryFacade {

    private DataSource dataSource;

    public CategoryFacade() throws InternalErrorException {
        dataSource = new SimpleDataSource();
    }

    public CategoryVO createCategory(CategoryVO categoryVO)
            throws InternalErrorException {

        try {
            CreateCategoryAction action = new CreateCategoryAction(categoryVO);
            return (CategoryVO) PlainActionProcessor.process(
                    dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } //createCategory

    public void deleteCategory(Long idCat)
            throws InstanceNotFoundException, InternalErrorException, InstanceCannotBeDeletedException {

        try {
            DeleteCategoryAction action = new DeleteCategoryAction(idCat);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
         } catch (InstanceCannotBeDeletedException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // deleteCategory

    public CategoryVO findCategoryById(Long idCat)
            throws InstanceNotFoundException, InternalErrorException {

        try {
            FindCategoryByIdAction action;
            action = new FindCategoryByIdAction(idCat);
            return (CategoryVO) PlainActionProcessor.process(
                    dataSource, action);
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // findCategoryById

    public CategoryVO findCategoryByName(String nomCat)
            throws InstanceNotFoundException, InternalErrorException {

        try {
            FindCategoryByNameAction action;
            action = new FindCategoryByNameAction(nomCat);
            return (CategoryVO) PlainActionProcessor.process(
                    dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // findCategoryByName

    public List<CategoryVO> getCategoryRoute(Long idCat)
            throws InstanceNotFoundException, InternalErrorException {

        try {
            GetCategoryRouteAction action;
            action = new GetCategoryRouteAction(idCat);
            return (List<CategoryVO>) PlainActionProcessor.process(
                    dataSource, action);
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // getCategoryRoute


    public void modifyCategory(Long idCat, String nomCat, Long catPadre)
            throws InstanceNotFoundException, InternalErrorException {

        try {
            ModifyCategoryAction action =
                    new ModifyCategoryAction(idCat, nomCat, catPadre);

            PlainActionProcessor.process(dataSource, action);
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // modifyCategory

    public CategoryChunkVO getCategorySons(long catPadre, int startIndex, int count)
            throws InternalErrorException {

        try {

            GetSonsCategoryAction action =
                    new GetSonsCategoryAction(catPadre, startIndex, count);

            return (CategoryChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // getCategorySons

}
